package com.cn.ryc.service;

import com.cn.ryc.mapper.DangerRecordMapper;
import com.cn.ryc.mapper.DetectionRecordMapper;
import com.cn.ryc.model.DangerRecord;
import com.cn.ryc.model.DetectionRecord;
import com.cn.ryc.util.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class DetectionService {

    @Autowired
    private DetectionRecordMapper detectionRecordMapper;

    @Autowired
    private DangerRecordMapper dangerRecordMapper;

    public ResponseEntity<ApiResponse<String>> insertDetectionRecords(String videoUrl, LocalDateTime detectionTime, List<Map<String, Object>> detections) {
        try {
            for (Map<String, Object> detection : detections) {
                String detectionClass = (String) detection.get("class");
                Double confidence = (Double) detection.get("confidence");
                Double x = (Double) detection.get("x");
                Double y = (Double) detection.get("y");
                Double width = (Double) detection.get("width");
                Double height = (Double) detection.get("height");


                DetectionRecord detectionRecord = new DetectionRecord(null, videoUrl, detectionTime, detectionClass, confidence, x, y, width, height);
                detectionRecordMapper.insertDetectionRecord(detectionRecord);

                Long detectionId = detectionRecord.getId();
                System.out.println("Detection ID: " + detectionId);

                if ("no_helmet".equals(detectionClass) || "no_hook".equals(detectionClass) 
                    || "smoking".equals(detectionClass) || "fire".equals(detectionClass)) {

                    if (detectionId == null) {
                        System.out.println("Warning: Detection ID is null, skipping danger record");
                        continue;
                    }

                    DangerRecord dangerRecord = new DangerRecord();
                    dangerRecord.setDetectionId(detectionId);
                    dangerRecord.setVideoUrl(videoUrl);
                    dangerRecord.setDetectionTime(detectionTime);
                    dangerRecord.setTime(detectionTime);
                    dangerRecord.setDetectionClass(detectionClass);
                    dangerRecord.setConfidence(confidence);
                    dangerRecord.setX(x);
                    dangerRecord.setY(y);
                    dangerRecord.setWidth(width);
                    dangerRecord.setHeight(height);
                    
                    dangerRecordMapper.insertDangerRecord(dangerRecord);
                }
            }
            return ResponseEntity.ok(ApiResponse.success("Records inserted successfully"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(ApiResponse.error(500, "服务器内部错误: " + e.getMessage()));
        }
    }
} 